<template>
  <div class="container">
    <div class="button">
      <el-button type="primary" size="small" @click="dialogFormVisible = true">添加分类</el-button>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column align="center" label="ID" width="180">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="分类名称" width="180"> </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="editNavCategory(scope.row._id, scope.row.name)"
              >编辑</el-button
            >
            <el-button type="danger" size="small" @click="deleteNavCategory(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" :before-close="beforeClose">
      <el-form ref="navicategoryForm" label-width="50px" :rules="rules" :model="form">
        <el-form-item label="分类名称" label-width="130px" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" label-width="130px" prop="sort">
          <el-input v-model="form.sort"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNavList, createNavCategory, updateNavCategory, deleteNavCategory } from '@/api/navigation'
export default {
  name: 'MyNavCategory',
  data() {
    return {
      tableData: [],
      title: '新增',
      dialogFormVisible: false,
      editId: '',
      form: {
        name: '',
        sort: 0
      },
      rules: {
        name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        sort: [
          { required: true, message: '请输入排序序号', trigger: 'blur' },
          { type: 'number', message: '排序序号必须为数字值' }
        ]
      }
    }
  },
  created() {
    this.getNavCategory()
  },
  methods: {
    // 获取导航分类列表
    async getNavCategory() {
      try {
        const res = await getNavList()
        if (res.code === 200) {
          this.tableData = res.data
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑导航分类
    editNavCategory(id, name) {
      this.title = '编辑'
      this.editId = id
      this.form.name = name
      this.dialogFormVisible = true
    },
    // 删除导航分类
    deleteNavCategory(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteNavCategory(id)
          if (res.code === 200) {
            this.$message.success({
              message: '删除成功',
              offset: 80
            })
          }
          this.getNavCategory()
        })
        .catch(() => {
          this.$message.info({
            message: '已取消删除',
            offset: 80
          })
        })
    },
    // 提交表单
    submitForm() {
      this.$refs.navicategoryForm.validate(async valid => {
        if (valid) {
          try {
            const res = this.editId
              ? await updateNavCategory(this.editId, this.form)
              : await createNavCategory(this.form)
            if (res.code === 200) {
              this.dialogFormVisible = false
              this.$message.success({
                message: this.editId ? '编辑成功' : '新增成功',
                offset: 80
              })
              this.getNavCategory()
            }
          } catch (error) {
            console.log(error)
          }
        }
      })
    },
    // 弹框取消
    cancel() {
      this.dialogFormVisible = false
      this.$refs.navicategoryForm.resetFields()
    },
    // 弹框关闭之前清空文件
    beforeClose(done) {
      this.$refs.navicategoryForm.resetFields()
      done()
    }
  }
}
</script>

<style lang="scss" scoped>
.button {
  display: flex;
  justify-content: flex-end;
  padding: 8px 8px 0 8px;
  background-color: #fff;
}
</style>
